package com.cm.figure.controller.pictureController;

import com.cm.figure.model.Data.Data;
import com.cm.figure.model.Data.Meta;
import com.cm.figure.model.DataMap.DataMap;
import com.cm.figure.model.Pageing;
import com.cm.figure.model.Pictures;
import com.cm.figure.model.Server;
import com.cm.figure.service.pictureService.PictureService;
import com.cm.figure.utility.BuildData;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:ImageController
 * Package:com.cm.figure.controller.pictureController
 * Description:
 *
 * @Date:2021/8/16 22:00
 * @com.chuangmei
 */
@Controller
@RequestMapping("/api/private/figure")
public class PictureController {

    @Autowired
    private PictureService pictureService;

    /**
     * 数据库中查询图片
     * @param userid
     * @return
     */
    @GetMapping("images/{userid}")
    @ResponseBody
    public Data images(@PathVariable Long userid) {

        // data
        Data data = null;

        // 通过userid查询对于用户下的图片 并设置图片Url
        Pictures[] pictures = this.setPicturesUrl(pictureService.queryPicturesByUserId(userid));

        // 如果图片数量大于0
        if (pictures.length > 0) {
            data = BuildData.buildData(pictures,"查询成功!" , 200);
        }else {
            data = BuildData.buildData(pictures,"暂未上传任何图片!" , 400);
        }

        return data;
    }

    /**
     * 删除图片
     * @param id
     * @return
     */
    @GetMapping("deletePicture/{id}")
    @ResponseBody
    public Data deletePicture(@PathVariable Long id) {

        int count = pictureService.removePictureById(id);

        return count > 1 ? BuildData.buildData(null,"删除成功!",200) : BuildData.buildData(null,"删除失败!",400);
    }


    /**
     * 分页
     */
    @GetMapping("pageing/{userid}")
    @ResponseBody
    public Map pageing
    (@PathVariable Long userid , String pageInfo) {

        JSONObject json = JSONObject.fromObject(pageInfo);
        Integer currentPage = (Integer) json.get("currentPage");
        Integer pageSize = (Integer) json.get("pageSize");
        Integer total = (Integer) json.get("total");

        Pageing pageing = new Pageing();
        pageing.setCurrentPage(currentPage.longValue());
        pageing.setPageSize(pageSize.longValue());
        pageing.setTotal(total.longValue());


        // 创建返回数据
        //DataMap dataMap = new DataMap();
        Map map = new HashMap();


        //用于存储查询的图片和total
        //Object[] response = new Object[2];

        // 返回信息
        String message = "查询失败!";
        // 返回状态码
        Integer status = 201;

        // 分页查询图片
        Pictures[] pictures = this.setPicturesUrl(pictureService.queryPicturesPageing(userid , pageing));
        map.put("pictures",pictures);
        map.put("total",pictureService.queryPicturesTotalByUserId(userid));
        /*dataMap.getData().put("pictures",pictures);
        dataMap.getData().put("total",pictureService.queryPicturesTotalByUserId(userid));*/

        // 判断是否查询成功
        if (pictures.length > 0) {
            message = "查询成功!";
            status = 200;
        }

        Meta meta = new Meta();
        meta.setMessage(message);
        meta.setStatus(status);

        map.put("meta",meta);

        //构建返回信息
        //dataMap = BuildData.buildData(pictures,message,status);
        //dataMap = BuildData.buildDataMap(message,status);
        return map;
    }

    private Pictures[] setPicturesUrl(Pictures[] pictures) {
        // 设置图片Url
        for (Pictures picture : pictures) {
            if (Server.isLocal) {
                picture.setUrl(Server.imagePathPrefixLocal + picture.getFilename());
            }else{
                picture.setUrl(Server.imagePathPrefixServer + picture.getFilename());
            }
        }
        return pictures;
    }

}
